import { Module } from "vuex";
// 该泛型需结合两个参数
import IProductState from "./type";
import { IRootState } from "@/store/types";
import {
  reqGetOrder,
  reqReviewOrders,
  reqGetOrderLimit,
  reqGetTotal,
  reqRefreshOrder
} from "@/service/main/product/product";
import { ElMessage } from "element-plus";
import { reactive } from "vue";
const systemModule: Module<IProductState, IRootState> = {
  // 提供一个作用域
  namespaced: true,
  state() {
    return reactive({
      data: [], //审核列表
      orderList: [], //all订单列表
      total: 1
    });
  },
  actions: {
    // 获取订单审核
    async getOrderList({ commit }) {
      const employeedata = await reqGetOrder();
      console.log(employeedata);

      if (employeedata.status !== 200) {
        return ElMessage.error(`服务器请求出错! ${employeedata.message}`);
      }
      if (employeedata.data.total === 200) {
        if(employeedata.data.data == Object) {
          console.log('!!!!!!!');
          commit("GETORDERLIST", employeedata.data.data);
        }
        return Promise.resolve("ok");
      } else {
        return Promise.reject(new Error("fail"));
      }
    },
    // 审核
    async reviewOrders({ commit }, data) {
      delete data.createTime;
      const res = await reqReviewOrders(data);
      console.log("审核", res);

      if (res.status === 200) {
        if (res.total == 200) {
          return Promise.resolve(true);
        }
      } else {
        return ElMessage.error(`服务器请求出错! ${res.message}`);
      }
    },
    // 订单列表
    async getOrderLimit(textent, data) {
      const res = await reqGetOrderLimit(data);
      textent.dispatch("getOrdercount");
      if (res.status === 200) {
        if (res.data.total == 200) {
          textent.commit("GETORDERLIMIT", res.data.data);
          return Promise.resolve(true);
        }
      } else {
        return ElMessage.error(`服务器请求出错! ${res.message}`);
      }
    },
    // 订单条数
    async getOrdercount({ commit }) {
      const res = await reqGetTotal();
      if (res.status == 200) {
        commit("GETORDERCOUNT", res.data);
      } else {
        return ElMessage.error(`服务器请求出错! ${res.message}`);
      }
    },
    // 刷新
    async refreshOrder() {
      //
      const res = await reqRefreshOrder();

      console.log("刷新", res);
    }
  },
  mutations: {
    GETORDERLIST(state, data) {
      state.data = data;
    },
    GETORDERLIMIT(state, data) {
      state.orderList = data;
    },
    GETORDERCOUNT(state, data) {
      state.total = data;
    }
  },
  getters: {
    getReviewOrdersList(state) {
      return state.data || [];
    },
    getOrderList(state) {
      return state.orderList || [];
    },
    getOrderTotal(state) {
      return state.total || [];
    }
  }
};
export default systemModule;
